package com.bw.unit3.view.activity

import android.content.Intent
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.unit3.Const
import com.bw.unit3.R
import com.bw.unit3.base.BaseActivity
import com.bw.unit3.bean.LoginResult
import com.bw.unit3.databinding.ActivityLoginBinding
import com.bw.unit3.http.HttpUtil
import com.bw.unit3.uitl.DBUtil
import com.bw.unit3.uitl.SpUtil
import com.bw.unit3.viewmodel.LoginViewModel
import com.bw.unit3.widgit.LoadingDialog


@Route(path = Const.PATH_LOGIN)
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>(R.layout.activity_login) {


//    @Autowired(name="userName")
//    lateinit var tt:String

    @Autowired
    lateinit var userName: String
    private var loginLoading: LoadingDialog? = null
    override fun initData() {
        viewModel.loginLiveResult.observe(this) {
            if (it is LoginResult.Start) {
                //显示loading
                loginLoading?.toDismiss()
                loginLoading = LoadingDialog(this, "正在登录")
                loginLoading?.toShow()

            } else if (it is LoginResult.Success) {
                loginLoading?.toDismiss()
                //登录成功，页面跳转
                //把当前登录的用户信息保存到room 数据库
                DBUtil.getShopDb().getLoginDao().insertLogin(it.resp)
                //保存token至本地
                SpUtil.putString("token", it.resp.token)
                ARouter.getInstance().build("/main/MainActivity").navigation()
//                SPUtils.getInstance().put("token", it.data.token)
            } else {
                loginLoading?.toDismiss()
                ToastUtils.showShort("登录失败")
            }
        }



        viewModel.loginLiveData.observe(this)
        {
            ToastUtils.showShort(it.message)
            if (it.code == 200) {
                //登录成功，页面跳转
                //把当前登录的用户信息保存到room 数据库
                DBUtil.getShopDb().getLoginDao().insertLogin(it.data)
                //保存token至本地
                SpUtil.putString("token", it.data.token)
//                SPUtils.getInstance().put("token", it.data.token)
            }
        }
    }


    override fun initView() {

//ARouter参数注入配置
        ARouter.getInstance().inject(this)
        ToastUtils.showShort("dd$userName")
        // 从数据库获取 当地登录的用户信息
        val loginBean = DBUtil.shoppingDb.getLoginDao().getLoginBean()
        if (loginBean != null) {
            dataBinding.etName.setText(loginBean.username)
        }

//        dataBinding.etName.setExtractedText()
        //处理登录点击事件
        dataBinding.btn.setOnClickListener {
            if (!HttpUtil.isNet(this@LoginActivity)) {
                ToastUtils.showShort("请开启网络")
                return@setOnClickListener
            }

            val name = dataBinding.etName.text.toString()
            val pass = dataBinding.etPass.text.toString()
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) {
                ToastUtils.showShort("用户名或密码不能为空")
                return@setOnClickListener
            }

            val map = HashMap<String, String>()
            map["username"] = name
            map["password"] = pass

            viewModel.login(map)
        }

        dataBinding.tvRegister.setOnClickListener {

            startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
        }

        //设置按钮是否可用
//        dataBinding.btn.isEnabled = false

        dataBinding.tvRegister.text = "dd"


    }
}